
import store from '@/store';
import * as types from '@/store/mutation-types.js'
import {
    sessionSetItem,
} from 'util/index.js';
import {
    getAuth
} from 'util/getAuth.js';
import { parseUA } from '@/util/index'

export default {
    // 标识 当前的hook的作用
    cancelToken: (to, from, next) => {
        // 清除token
        store.commit(types.CLEAR_TOKEN); // 清除所有的请求
        next(); // 继续王下走
    },
    permission: (to, from, next) => {
        console.log('从哪里进入的');
        // 保存起来从哪里进入的  方便登录页的返回逻辑处理
        console.log(from);
        sessionSetItem('fromRoute', from.path);
        if (to.meta.noAuth) {
            console.log(`router跳转前拦截不需要验证token是否存在`);
            next();
        } else {
            const token = store.state.token;
            // 如果token存在
            if (token) {
                next()
            } else {
                console.log(`router跳转前拦截token不存在，fullpath为${to.fullPath}`);
                let redirect = to.fullPath;
                sessionSetItem('redirect', redirect); // 在登录成功后取出来
                next({ path: '/login', replace: true });
            }
        }
    },
    wxConfig: (to, from, next) => {
        if (parseUA().ios) { // 苹果手机
            console.log('routerBefore中获取到的url' + location.href);
            let initLink = sessionStorage.getItem("initLink");
            // let url = location.href.split('#')[0];
            let url = location.href;
            if (to.path == '/') {
                sessionStorage.setItem("initLink", `${url}index`);
            } else {
                if (!initLink) {
                    sessionStorage.setItem("initLink", url);
                } else {
                    console.log('initLink已经存在了');
                }
            }
            next()
        } else {
            next()
        }
    }
}
